package com.tiyende.kash.loop.ui.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import androidx.recyclerview.widget.RecyclerView
import com.lib.common.adapter.CommonRecyclerViewAdapter
import com.lib.common.adapter.CommonViewHolderHelper
import com.tiyende.kash.loop.R
import com.tiyende.kash.loop.TiyendeApp
import com.tiyende.kash.loop.utils.TiyendeDateUtil
import com.tiyende.kash.loop.weight.font.RobotoBlackTextView
import com.tiyende.kash.loop.weight.font.RobotoBoldTextView
import com.tiyende.kash.loop.weight.font.RobotoMediumButton
import org.json.JSONObject
import java.lang.Exception
import java.math.BigDecimal

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/19 10:43
 * @Annotation：订单列表适配器
 */
class TiyendeOrderListAdapter(recyclerView: RecyclerView?) : CommonRecyclerViewAdapter<JSONObject>(recyclerView) {

    override fun getItemViewType(position: Int): Int {
        val model = getItem(position)
        return when (model.optBoolean("isEmptyView",false)){
            true -> {
                R.layout.tiyende_activity_order_list_empty
            }
            else -> {
                R.layout.tiyende_activity_order_list_item
            }
        }
    }

    override fun setItemChildListener(helper: CommonViewHolderHelper, viewType: Int) {
        helper.setItemChildClickListener(R.id.btnOrderDetails)
    }

    override fun fillData(helper: CommonViewHolderHelper, position: Int, model: JSONObject) {
        when (model.optBoolean("isEmptyView",false)){
            true -> {//空
//                val textWithdraw = helper.getTextView(R.id.textWithdraw)
//                textWithdraw.paint.flags = Paint.UNDERLINE_TEXT_FLAG//下划线
//                textWithdraw.paint.isAntiAlias = true//抗锯齿
            }
            else -> {
                showTiyendeOrderItemView(helper,position,model)
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun showTiyendeOrderItemView(helper: CommonViewHolderHelper, position: Int, model: JSONObject){
        val textOrderStatus = helper.getView<RobotoBoldTextView>(R.id.textOrderStatus)
        val textApplyAmount = helper.getTextView(R.id.textApplyAmount)
        val textApplyTime = helper.getView<RobotoBlackTextView>(R.id.textApplyTime)
        val btnOrderDetails = helper.getView<RobotoMediumButton>(R.id.btnOrderDetails)

        val loanAmount = BigDecimal(model.optString("loanAmount")).stripTrailingZeros().toPlainString()
        textApplyAmount.text = "$loanAmount ${model.optString("currency")}"

        var showApplyDate = model.optString("applyDate")
        showApplyDate = try {
            TiyendeDateUtil.getFormatTime(TiyendeDateUtil.convertDateToTimestamp("MM/dd/yyyy",showApplyDate),"dd/MM/yyyy")
        } catch (e: Exception){
            model.optString("applyDate")
        }
        textApplyTime.text = showApplyDate

        val orderStatus = model.optString("orderStatus")
        when(orderStatus){
            "IN_REVIEW" -> {//审核中
                textOrderStatus.text = TiyendeApp.app.getString(R.string.tiyende_order_status_under_review)
                textOrderStatus.shapeDrawableBuilder.setSolidColor(Color.parseColor("#FF3ED9AB")).intoBackground()
                textApplyAmount.setTextColor(Color.parseColor("#FFFFFFFF"))
                textApplyTime.setTextColor(Color.parseColor("#FFFFFFFF"))
                btnOrderDetails.shapeDrawableBuilder
                    .setSolidColor(Color.parseColor("#FF3ED9AB"))
                    .intoBackground()
            }
            "LENDING" -> {//放款中
                textOrderStatus.text = TiyendeApp.app.getString(R.string.tiyende_order_status_payment_in_progress)
                textOrderStatus.shapeDrawableBuilder.setSolidColor(Color.parseColor("#FFFF850A")).intoBackground()
                textApplyAmount.setTextColor(Color.parseColor("#FFFFFFFF"))
                textApplyTime.setTextColor(Color.parseColor("#FFFFFFFF"))
                btnOrderDetails.shapeDrawableBuilder
                    .setSolidColor(Color.parseColor("#FF3ED9AB"))
                    .intoBackground()
            }
            "REJECT" -> {//已拒绝
                textOrderStatus.text = TiyendeApp.app.getString(R.string.tiyende_order_status_rejected)
                textOrderStatus.shapeDrawableBuilder.setSolidColor(Color.parseColor("#FFF36767")).intoBackground()
                textApplyAmount.setTextColor(Color.parseColor("#FFFFFFFF"))
                textApplyTime.setTextColor(Color.parseColor("#FFFF7A6B"))
                btnOrderDetails.shapeDrawableBuilder
                    .setSolidColor(Color.parseColor("#FFF36767"))
                    .intoBackground()
            }
        }
    }
}